package pkg

import (
	"china-auto-rental/config"
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func Update(file *multipart.FileHeader) (string, error) {
	yun := config.Conf.QiNuYun
	accessKey := yun.AccessKey
	secretKey := yun.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)

	localFile := yun.LocalFile
	bucket := yun.Bucket
	key := file.Filename

	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadFile(context.Background(), localFile+key, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	return localFile + key, err
}
